package ee.maoinvest.frontend.controller.command.electricitymetercommands;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import ee.maoinvest.backend.model.data.ElectricityContract;
import ee.maoinvest.backend.model.data.TransformerCoefficentStandard;
import ee.maoinvest.backend.services.ElectricityContractService;
import ee.maoinvest.backend.services.TransformerCoefficentStandardService;
import ee.maoinvest.backend.services.implementations.ElectricityContractServiceImpl;
import ee.maoinvest.backend.services.implementations.TransformerCoefficentStandardServiceImpl;
import ee.maoinvest.frontend.controller.command.Command;
import ee.maoinvest.frontend.controller.formstringdataconverters.electricitymeterforms.InsertOuterTerritoryElectricityMeterFormConverter;

public class FillInsertOuterTerritoryElectricityMeterFormConverterWithInitialDataCommand implements Command
{
	private Logger logger = Logger.getLogger(FillInsertOuterTerritoryElectricityMeterFormConverterWithInitialDataCommand.class);
	private int operationResult = 0;
	private final int OPERATION_SUCCESSFUL = 1;
	private final int PROBABLY_A_DATABASE_ERROR = -1;
	
	@Override
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		InsertOuterTerritoryElectricityMeterFormConverter insertOuterTerritoryElectricityMeterFormConverter = null;
		List<TransformerCoefficentStandard> listOfTransformerCoefficentStandardsNotWrittenOff = null;
		List<ElectricityContract> listOfElectricityContractsForOuterTerritoryMeters = null;
		TransformerCoefficentStandardService transformerCoefficentStandardService = null;
		ElectricityContractService electricityContractService = null;
		
		try 
		{
			insertOuterTerritoryElectricityMeterFormConverter = new InsertOuterTerritoryElectricityMeterFormConverter();
			transformerCoefficentStandardService = new TransformerCoefficentStandardServiceImpl();
			electricityContractService = new ElectricityContractServiceImpl();
			listOfTransformerCoefficentStandardsNotWrittenOff = new ArrayList<TransformerCoefficentStandard>();
			listOfElectricityContractsForOuterTerritoryMeters = new ArrayList<ElectricityContract>();
			listOfTransformerCoefficentStandardsNotWrittenOff = transformerCoefficentStandardService.getAllTransformerCoefficentStandardsThatAreNotWrittenOff();
			listOfElectricityContractsForOuterTerritoryMeters = electricityContractService.getAllElectricityContractsForOuterTerritoryMetersThatAreNotWrittenOff();
			
			insertOuterTerritoryElectricityMeterFormConverter.setInitialDataIntoConverter(listOfElectricityContractsForOuterTerritoryMeters, 
					listOfTransformerCoefficentStandardsNotWrittenOff);
			request.setAttribute("insertOuterTerritoryElectricityMeterFormConverter", insertOuterTerritoryElectricityMeterFormConverter);
			
			HttpSession session = request.getSession();
			session.setAttribute("listOfTransformerCoefficentStandardsNotWrittenOff", listOfTransformerCoefficentStandardsNotWrittenOff);
			session.setAttribute("listOfElectricityContractsForOuterTerritoryMeters", listOfElectricityContractsForOuterTerritoryMeters);
			
			operationResult = OPERATION_SUCCESSFUL;
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			logger.error(e.getMessage());
			return PROBABLY_A_DATABASE_ERROR;
		}
		
		return operationResult;
	}
}
